package ConfigProvider

import (
	cfg "Volta/Config"
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
	"path/filepath"
	"time"
)

type Config struct{
	ConfigFile string
}

func (config *Config) Handle() error{
	var status string
	_, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		status = "dir nil"
	}
	serverFile,err := ioutil.ReadFile(config.ConfigFile)
	if err != nil {
		status = "not find server.yaml"
	}
	macFile,err := ioutil.ReadFile("CronJob/machine.yaml")
	if err != nil {
		status = "not find machine.yaml"
	}
	databaseConfErr := yaml.Unmarshal(serverFile,&cfg.Database)
	if databaseConfErr != nil {
		status = "no database config"
	}
	serverConfErr := yaml.Unmarshal(serverFile,&cfg.Server)
	if serverConfErr != nil {
		status = "no server config"
	}
	macConfErr := yaml.Unmarshal(macFile,&cfg.Mac)
	if macConfErr != nil {
		status = "no mac config"
	}
	if status != "" {
		fmt.Println(status)
		time.Sleep(time.Second*3)
		os.Exit(0)
	}
	return nil
}
